<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Cities;
use App\Models\FilmTypeMaster;
use App\Models\MacroManagement;
use App\Models\MakeMaster;
use App\Models\ModelMaster;
use App\Models\PartMaster;
use App\Models\ProvinceMaster;
use App\Models\RegionMaster;
use App\Models\Roles;
use App\Models\Permissions;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

/*
 * 角色管理*/
class RolesController extends Controller
{
    
    public function index(Request $request)
    {
        $page = [
            'page_name' => 'roles',
            'title' => ts('jsgl'),
        ];
        $data = Roles::orderBy('id')->get();

        return view('admin.roles.index', [
            'data' => $data,
            'page' => $page
        ]);
    }

    public function show(Request $request, $id)
    {
        $page = [
            'page_name' => 'roles',
            'title' => ts('xq'),
            'breadcrumb' => [
                [
                    'text' => ts('jsgl'),
                    'url' => '/admin/roles'
                ]
            ],
        ];
        $data = Roles::findOrFail($id);
        # $permission = $this->permission($id);
        $permission = $this->permission2($id);
        return view('admin.roles.show', [
            'data' => $data,
            'page' => $page,
            'permission' => $permission,
        ]);
    }

    public function create()
    {
        $page = [
            'page_name' => 'roles',
            'title' => ts('addnew'),
            'breadcrumb' => [
                [
                    'text' => ts('jsgl'),
                    'url' => '/admin/roles'
                ]
            ],
        ];
        $data = new Roles();
        // $permission = $this->permission();

        $permission = $this->permission2();
        return view('admin.roles.create', [
            'data' => $data,
            'page' => $page,
            'permission' => $permission,
        ]);
    }

    public function store(Request $request)
    {
        $permissions = $request->input('permissions');
        $name = trim($request->input('name'));
        $model = new Roles();
        $model->name = $name;
        $model->edit_able = 1;
        $model->save();
        $model->permissions()->sync($permissions);

        return redirect('/admin/roles')->with('trash', '角色新增成功！');
    }

    public function edit(Request $request, $id)
    {
        $page = [
            'page_name' => 'roles',
            'title' => ts('xg'),
            'breadcrumb' => [
                [
                    'text' => ts('jsgl'),
                    'url' => '/admin/roles'
                ]
            ],
        ];
        if ($id <= 3) {
            return redirect('/admin/roles/')->with('trash', ['type'=>'error', 'content'=>'系统自带角色不可以修改']);
        }
        $data = Roles::findOrFail($id);
        // $permission = $this->permission($id);
        $permission = $this->permission2($id);
        return view('admin.roles.edit', [
            'data' => $data,
            'page' => $page,
            'permission' => $permission,
        ]);
    }

    public function update(Request $request, $id)
    {
        if ($id <= 3) {
            return redirect('/admin/roles/')->with('trash', ['type'=>'error', 'content'=>'系统自带角色不可以修改']);
        }
        $permissions = $request->input('permissions');
        $name = trim($request->input('name'));
        $model = Roles::findOrFail($id);
        $model->name = $name;
        $model->save();
        $model->permissions()->sync($permissions);

        return redirect('/admin/roles')->with('trash', '角色更新成功！');

    }

    protected function permission($role_id=0)
    {
        // $role_id = (int) $role_id ?: session('admin.id');

        $pids = [];
        if (!empty($role_id)) {
            $role = Roles::findOrNew($role_id);
            $ps = $role->permissions()->get(['id', 'simplified_chiness_value']);
            $pids = Arr::pluck($ps, 'id');
        }
        
        $data = Permissions::orderBy('id')->get();
        $p1 = [];
        $p2 = [];
        foreach ($data as $k => $v) {
            $p1[$v->id] = $v->simplified_chiness_value;
            if ($v->parent_id != 0) {
                $p2[$v->id]['label'] = $v->simplified_chiness_value;
                $p2[$v->id]['value'] = $v->id;

                if (in_array($v->id, $pids)) {
                    $p2[$v->id]['checked'] = 1;
                }
            }
        }

        /*foreach ($data as $k => $v) {
            $p1[$v->id] = $v->simplified_chiness_value;
            if ($v->parent_id == 0) {
                if (empty($p2[$v->id])) {
                    $p2[$v->id] = [];
                }
                $p2[$v->id]['label'] = $v->simplified_chiness_value;
                $p2[$v->id]['value'] = $v->id;
                $p2[$v->id]['children'] = @$p2[$v->id]['children'] ?: [];
            } else {
                if (empty($v->parent_id)) {
                    $p2[$v->parent_id] = [];
                }
                $p2[$v->parent_id]['label'] = @$p1[$v->parent_id];
                $p2[$v->parent_id]['value'] = $v->parent_id;
                $p2[$v->parent_id]['children'] = @$p2[$v->parent_id]['children'] ?: [];
                $item = [
                    'label' => $v->simplified_chiness_value,
                    'value' => $v->id,
                ];
                if (in_array($v->id, $pids)) {
                    $p2[$v->parent_id]['checked'] = 1;
                    $item['checked'] = 1;
                }
                $p2[$v->parent_id]['children'][] = $item;
            }
        }*/
        //foreach ()
        return $p2;

    }

    protected function permission2($role_id=0)
    {
        // $role_id = (int) $role_id ?: session('admin.id');

        $pids = [];
        if (!empty($role_id)) {
            $role = Roles::findOrNew($role_id);
            $ps = $role->permissions()->get(['id', 'simplified_chiness_value']);
            $pids = Arr::pluck($ps, 'id');
        }

        $data = Permissions::orderBy('id')->get();
        $p1 = [];
        $p2 = [];
        foreach ($data as $k => $v) {
            $p1[$v->id] = $v->simplified_chiness_value;
            if ($v->parent_id == 0) {
                $p2[$v->id]['label'] = $v->simplified_chiness_value;
                $p2[$v->id]['value'] = $v->id;

                if (in_array($v->id, $pids)) {
                    $p2[$v->id]['checked'] = 1;
                }
            }
        }

        foreach ($data as $k => $v) {
            $p1[$v->id] = $v->simplified_chiness_value;
            if ($v->parent_id == 0) {
                if (empty($p2[$v->id])) {
                    $p2[$v->id] = [];
                }
                $p2[$v->id]['label'] = $v->simplified_chiness_value;
                $p2[$v->id]['value'] = $v->id;
                $p2[$v->id]['children'] = @$p2[$v->id]['children'] ?: [];
            } else {
                if (empty($v->parent_id)) {
                    $p2[$v->parent_id] = [];
                }
                $p2[$v->parent_id]['label'] = @$p1[$v->parent_id];
                $p2[$v->parent_id]['value'] = $v->parent_id;
                $p2[$v->parent_id]['children'] = @$p2[$v->parent_id]['children'] ?: [];
                $item = [
                    'label' => $v->simplified_chiness_value,
                    'value' => $v->id,
                ];
                if (in_array($v->id, $pids)) {
                    $p2[$v->parent_id]['checked'] = 1;
                    $item['checked'] = 1;
                }
                $p2[$v->parent_id]['children'][] = $item;
            }
        }
        //foreach ()
        return $p2;
    }
}
